package com.mdlib.helper.app;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.ljlib.help.R;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.service.MDPushIntentService;
import com.mdlib.helper.ui.widget.GlideImageLoader;
import com.mdlib.helper.utils.GlidePauseOnScrollListener;
import com.orhanobut.logger.Logger;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by librabin on 16/10/12.
 */

public class AppContext extends Application {

    private static AppContext instance;
    public static String packname;
    public static String version;


    private String sessionId;


    @Override
    public void onCreate() {
        super.onCreate();
        initSpeech();
        instance = this;
        packname = getPackageInfo().packageName;
        version = getVersion();
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            // This process is dedicated to LeakCanary for heap analysis.
//            // You should not init your app in this process.
//            return;
//        }
//        PushAgent mPushAgent = PushAgent.getInstance(this);
        JPushInterface.init(this);
        //注册推送服务，每次调用register方法都会回调该接口
//        mPushAgent.register(new IUmengRegisterCallback() {
//
//            @Override
//            public void onSuccess(String deviceToken) {
//                //注册成功会返回device token
//               Logger.e(deviceToken);
//                if (deviceToken != null) {
//                    AppModel.getInstance().setPushToken(deviceToken);
//                }
//            }
//
//            @Override
//            public void onFailure(String s, String s1) {
//                Logger.e(s);
//            }
//
//
//        });
//        mPushAgent.setPushIntentServiceClass(MDPushIntentService.class);
//        LeakCanary.install(this);
        galleryFinal();
    }

    private void galleryFinal() {
        //建议在application中配置
        //设置主题
        ThemeConfig theme = new ThemeConfig.Builder()
                .setTitleBarBgColor(getResources().getColor(R.color.myred))
                .setTitleBarTextColor(getResources().getColor(R.color.generic_color_white))
                .setTitleBarIconColor(getResources().getColor(R.color.generic_color_white))
                .setFabNornalColor(getResources().getColor(R.color.myred))
                .setFabPressedColor(getResources().getColor(R.color.generic_color_tv_tips))
                .setCheckNornalColor(getResources().getColor(R.color.generic_color_tv_tips))
                .setCheckSelectedColor(getResources().getColor(R.color.myred))
                .setIconBack(R.drawable.general_back_b)
//                .setIconRotate(R.mipmap.ic_action_repeat)
//                .setIconCrop(R.mipmap.ic_action_crop)
//                .setIconCamera(R.mipmap.ic_action_camera)
                .build();
        //配置功能
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(false)
                .setEnableCrop(true)
                .setEnableRotate(true)
                .setCropSquare(true)
                .setEnablePreview(false)
                .build();
        CoreConfig coreConfig = new CoreConfig.Builder(this, new GlideImageLoader(), theme)
                .setFunctionConfig(functionConfig)
                .setPauseOnScrollListener(new GlidePauseOnScrollListener(false, true))
                .build();
        GalleryFinal.init(coreConfig);
    }


    /**
     * 获得当前app运行的AppContext
     *
     * @return
     */
    public static AppContext getInstance() {
        return instance;
    }


    public String getSessionId() {
        return AccountModel.getInstance().getSessionId();
    }

    /**
     * 获取App安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0.0";
        }
    }
    private void initSpeech() {
        // 将“12345678”替换成您申请的 APPID，申请地址： http://www.xfyun.cn
        // 请勿在 “ =”与 appid 之间添加任务空字符或者转义符
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58fdce98" + SpeechConstant.FORCE_LOGIN + "=true");
    }
}
